<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of candidate_requirements_controller
 *
 * @author wilson
 */
class PropertyRequirementscontroller extends AppController {

    //put your code here
    var $name = "PropertyRequirements";

    function index($id = null) {

        $this->layout = "ajax";
        $this->loadRequirements(3, $id);
        $requerimientos = $this->PropertyRequirement->find('all',array('recursive'=>0, 'conditions'=>array('PropertyRequirement.property_id'=>$id,'PropertyRequirement.claim_id'=>0),'fields'=>array('PropertyRequirement.id' ,'Requirement.texto', 'PropertyRequirement.calificacion')));
        $this->set('requisitos', $requerimientos);
        $this->set('predio', $this->PropertyRequirement->Property->find('first', array('recursive' => -1, 'conditions' => array('Property.id' => $id), 'fields' => array('Property.codigo', 'Property.nombre'))));
    }

    function edit($id = null) {
        $this->set('est', $this->Session->read('estado'));
        if (empty($this->data)) {
            App::import('Model', 'RequirementFault');
            $ava = new RequirementFault();
            $this->data = $this->PropertyRequirement->find('first', array('conditions' => array('PropertyRequirement.id' => $id), 'fields' => array('PropertyRequirement.id', 'PropertyRequirement.requirement_id', 'PropertyRequirement.property_id', 'PropertyRequirement.concepto', 'PropertyRequirement.calificacion', 'Property.nombre', 'Property.codigo', 'Requirement.texto')));
            $this->set('chequeados', $this->PropertyRequirement->PropertyRequirementsRequirementFault->find("all", array("conditions" => array("PropertyRequirementsRequirementFault.property_requirement_id" => $id))));
            //$this->set('chequeados', $this->PropertyRequirement->PropertyRequirementsRequirementFault->findAllByPropertyRequirementId($id));
            $this->set('condiciones', $ava->find('all', array('conditions' => array('RequirementFault.requirement_id' => $this->data['PropertyRequirement']['requirement_id']), 'order' => array('RequirementFault.tipo' => 'asc'), 'fields' => array('RequirementFault.nombre', 'RequirementFault.tipo', 'RequirementFault.id'))));
        } else {
            $datos = $this->params['form'];
            $this->data['RequirementFault'] = array();
            if ($this->PropertyRequirement->save($this->data)) {
                $ident = $this->PropertyRequirement->id;
                $ide = $this->data['PropertyRequirement']['property_id'];
                $this->PropertyRequirement->PropertyRequirementsRequirementFault->deleteAll(array('PropertyRequirementsRequirementFault.Property_requirement_id' => $ident));
                $flag = 0;
                foreach ($datos['checked'] as $value) {
                    if ($value != 0) {
                        $flag = 1;
                        $this->PropertyRequirement->PropertyRequirementsRequirementFault->query("INSERT INTO property_requirements_requirement_faults (requirement_fault_id,property_requirement_id) values($value,$ident)");
                    }
                }
                if ($flag == 0) {
                    $this->PropertyRequirement->updateAll(array('PropertyRequirement.calificacion' => 0), array('PropertyRequirement.id' => $ident));
                }


                $this->Session->setFlash('Registro guardado exitosamente');

                $this->setAction('index', $ide);
            }
        }
    }

    function loadRequirements($type, $id) {

        $this->PropertyRequirement->Requirement->recursive = -1;
        $call_id = $this->Session->read('call_id');
        $requerimientos = $this->PropertyRequirement->Requirement->find(
                'all', array(
            'conditions' => array('Requirement.requirement_type_id' => $type, 'Requirement.call_id' => $call_id),
            'fields' => array('Requirement.id'),
            'recursive' => -1,
                ));
        $proyecto = $this->Session->read('proy_id');
        foreach ($requerimientos as $req) {


            $r = $req['Requirement']['id'];


            $cont = $this->PropertyRequirement->find('count', array('recursive' => -1, 'conditions' => array('PropertyRequirement.requirement_id' => $req['Requirement']['id'], 'PropertyRequirement.property_id' => $id)));
            if ($cont == 0) {

                $this->PropertyRequirement->query("INSERT INTO property_requirements (property_id,requirement_id,claim_id)values($id,$r,0)");
            }
        }
    }

    function edit_correction($id) {
        $this->set('est', $this->Session->read('estado'));
        if (empty($this->data)) {
            $this->data = $this->PropertyRequirement->find('first', array('conditions' => array('PropertyRequirement.id' => $id)));
        } else {



            if ($this->PropertyRequirement->save($this->data)) {
                $this->Session->setFlash('Registro guardado exitosamente');
                $this->redirect(array('controller' => 'CorrectionLetters', 'action' => 'index'));
            }
        }
    }

    function reevaluation_index($id = null, $claim_id) {

        $this->layout = "ajax";
        $this->set('claim_id', $claim_id);
        $this->PropertyRequirement->Requirement->recursive = -1;
        $requerimientos = $this->PropertyRequirement->Requirement->query("SELECT id FROM requirements Requirement WHERE Requirement.requirement_type_id=3");
        $proyecto = $this->Session->read('proy_id');
        foreach ($requerimientos as $req) {


            $r = $req['Requirement']['id'];


            $cont = $this->PropertyRequirement->find('count', array('conditions' => array('PropertyRequirement.requirement_id' => $req['Requirement']['id'], 'PropertyRequirement.property_id' => $id, 'PropertyRequirement.claim_id' => $claim_id)));
            if ($cont == 0) {

                $this->PropertyRequirement->query("INSERT INTO property_requirements (property_id,requirement_id,claim_id)values($id,$r,$claim_id)");
            }
        }

        $requerimientos = $this->PropertyRequirement->query("SELECT PropertyRequirement.`id` ,Requirement.texto, PropertyRequirement.`calificacion` FROM `property_requirements` PropertyRequirement LEFT JOIN requirements Requirement ON PropertyRequirement.`requirement_id` = Requirement.id WHERE property_id=$id and claim_id=$claim_id");
        $this->set('requisitos', $requerimientos);
        $this->set('predio', $this->PropertyRequirement->Property->find('first', array('conditions' => array('Property.id' => $id))));
    }

    function edit_reevaluation($id = null, $claim_id) {
        $this->set('est', $this->Session->read('estado'));
        $this->set('claim_id', $claim_id);
        if (empty($this->data)) {
            App::import('Model', 'RequirementFault');
            $ava = new RequirementFault();
            $this->data = $this->PropertyRequirement->find('first', array('conditions' => array('PropertyRequirement.id' => $id), 'fields' => array('PropertyRequirement.id', 'PropertyRequirement.requirement_id', 'PropertyRequirement.property_id', 'PropertyRequirement.concepto', 'PropertyRequirement.calificacion', 'Property.nombre', 'Property.codigo', 'Requirement.texto')));
            $this->set('chequeados', $this->PropertyRequirement->PropertyRequirementsRequirementFault->find("all", array("conditions" => array("PropertyRequirementsRequirementFault.property_requirement_id" => $id))));
            //$this->set('chequeados', $this->PropertyRequirement->PropertyRequirementsRequirementFault->findAllByPropertyRequirementId($id));
            $this->set('condiciones', $ava->find('all', array('conditions' => array('RequirementFault.requirement_id' => $this->data['PropertyRequirement']['requirement_id']), 'order' => array('RequirementFault.tipo' => 'asc'), 'fields' => array('RequirementFault.nombre', 'RequirementFault.tipo', 'RequirementFault.id'))));
        } else {
            $datos = $this->params['form'];
            $this->data['RequirementFault'] = array();
            if ($this->PropertyRequirement->save($this->data)) {
                $ident = $this->PropertyRequirement->id;
                $ide = $this->data['PropertyRequirement']['property_id'];
                $this->PropertyRequirement->PropertyRequirementsRequirementFault->deleteAll(array('PropertyRequirementsRequirementFault.property_requirement_id' => $ident));
                $flag = 0;
                foreach ($datos['checked'] as $value) {
                    if ($value != 0) {
                        $flag = 1;
                        $this->PropertyRequirement->PropertyRequirementsRequirementFault->query("INSERT INTO property_requirements_requirement_faults (requirement_fault_id,property_requirement_id) values($value,$ident)");
                    }
                }
                if ($flag == 0) {
                    $this->PropertyRequirement->updateAll(array('PropertyRequirement.calificacion' => 0), array('PropertyRequirement.id' => $ident));
                }


                $this->Session->setFlash('Registro guardado exitosamente');

                $this->setAction('reevaluation_index', $ide, $claim_id);
            }
        }
    }

}

?>
